import axios from 'axios';
import { Message } from 'element-ui';
import { getToken } from '@/utils/auth'

axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
// 创建 axios 实例
const service = axios.create({
  baseURL: '/api',
  // withCredentials: true,
  timeout: 60000
});

// 请求拦截器
service.interceptors.request.use(config => {
  config.headers['ms_username'] = localStorage.getItem('ms_username');
  if (getToken()) {
    // 让每个请求携带自定义token 请根据实际情况自行修改
    config.headers['Authorization'] = 'Bearer ' + getToken()
  }
  return config;
}, error => {
  console.log('request error',error);
  return Promise.reject(error);
});

// 响应拦截器
service.interceptors.response.use(response => {
  return response;
}, error => {
  console.log('response error', error);
  Message({
    message: error.message,
    type: 'error'
  });
  return Promise.reject(error);
});

export default service;